package com.autumn.zero.authorization.configure;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.autumn.auditing.IClientInfoProvider;
import com.autumn.security.user.IAutumnUserCredentialsService;
import com.autumn.zero.authorization.application.services.IRoleAppService;
import com.autumn.zero.authorization.application.services.IUserAppService;
import com.autumn.zero.authorization.application.services.IUserLoginAttemptAppService;
import com.autumn.zero.authorization.application.services.IUserLoginLogAppService;
import com.autumn.zero.authorization.application.services.impl.RoleAppServiceImpl;
import com.autumn.zero.authorization.application.services.impl.UserAppServiceImpl;
import com.autumn.zero.authorization.application.services.impl.UserLoginAttemptAppServiceImpl;
import com.autumn.zero.authorization.application.services.impl.UserLoginLogAppServiceImpl;
import com.autumn.zero.authorization.credential.UserCredentialsService;
import com.autumn.zero.authorization.entitys.roles.Role;
import com.autumn.zero.authorization.entitys.users.User;
import com.autumn.zero.authorization.services.IAuthorizationService;
import com.autumn.zero.authorization.services.impl.AuthorizationServiceImpl;

/**
 * 
 * 授权默认模板配置
 * 
 * @author 杨昌国 2018-12-07 19:36:47
 */
@Configuration
public class AutumnDefaultZeroAuthorizationConfiguration {

	/**
	 * 授权服务
	 * 
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean(IAuthorizationService.class)
	public IAuthorizationService autumnZeroAuthorizationService() {
		return new AuthorizationServiceImpl();
	}

	/**
	 * 用户应用服务
	 * 
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean(IUserAppService.class)
	public IUserAppService autumnZeroUserAppService() {
		return new UserAppServiceImpl();
	}

	/**
	 * 角色应用服务
	 * 
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean(IRoleAppService.class)
	public IRoleAppService autumnZeroRoleAppService() {
		return new RoleAppServiceImpl();
	}

	/**
	 * 用户登录日志
	 * 
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean(IUserLoginLogAppService.class)
	public IUserLoginLogAppService autumnZeroUserLoginLogAppService() {
		return new UserLoginLogAppServiceImpl();
	}

	/**
	 * 用户登录尝试
	 * 
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean(IUserLoginAttemptAppService.class)
	public IUserLoginAttemptAppService autumnZeroUserLoginAttemptAppService() {
		return new UserLoginAttemptAppServiceImpl();
	}

	/**
	 * 用户认证
	 * 
	 * @param clientInfoProvider 客户端信息提供程序
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean(IAutumnUserCredentialsService.class)
	public IAutumnUserCredentialsService autumnUserCredentialsService(IClientInfoProvider clientInfoProvider) {
		return new UserCredentialsService<User, Role>(this.autumnZeroAuthorizationService(), clientInfoProvider);
	}
}
